package com.ecoolex.ying.space.api.graphql;

import com.alibaba.fastjson.JSON;
import com.ecoolex.framework.common.util.Check;
import com.ecoolex.framework.modularize.api.service.storage.StorageInfo;
import com.ecoolex.framework.modularize.api.service.storage.StorageService;
import com.ecoolex.framework.web.api.graphql2.annotation.GraphQL2Module;
import com.ecoolex.framework.web.api.graphql2.annotation.Source;
import com.ecoolex.framework.web.api.graphql2.annotation.TypeField;
import com.ecoolex.ying.space.entity.*;
import com.ecoolex.ying.space.ws.GoodsService;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;
import java.util.stream.Collectors;

@GraphQL2Module(type = GraphQL2Module.Type.User, description = "商品分类查询")
public class GoodsGraphql {
    @Reference
    private GoodsService goodsService;
    @Autowired
    private StorageService storageService;

    @TypeField(type = GoodsWithBLOBs.class, field = "brand", description = "品牌")
    public GoodsBrand brand(@Source GoodsWithBLOBs source) {
        return Check.isNull(source.getBrandId()) ? null : goodsService.getBrandById(source.getBrandId());
    }

    @TypeField(type = GoodsWithBLOBs.class, field = "attachmentList", description = "附件列表")
    public List<GoodsAttachment> attachmentList(@Source GoodsWithBLOBs source) {
        return goodsService.queryAttachmentByGoodsId(source.getGoodsId());
    }

    @TypeField(type = GoodsAttachment.class, field = "storageList", description = "存储文件列表")
    public List<StorageInfo> storageIds(@Source GoodsAttachment source) {
        return JSON.parseArray(source.getStorageIds(), String.class).stream()
                .map(storageService::get)
                .filter(Check::notNull)
                .collect(Collectors.toList());
    }

    @TypeField(type = GoodsWithBLOBs.class, field = "specGroupList", description = "规格分组列表")
    public List<GoodsSpecGroup> specGroupList(@Source GoodsWithBLOBs source) {
        return goodsService.querySpecGroupByGoodsId(source.getGoodsId());
    }

    @TypeField(type = GoodsSpecGroup.class, field = "specList", description = "规格列表")
    public List<GoodsSpec> specList(@Source GoodsSpecGroup source) {
        return goodsService.querySpecByGroupId(source.getSpecGroupId());
    }
}
